using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 虚弱Buff - 降低目标攻击���
/// </summary>
public class WeaknessBuff : BuffBase
{
    private float m_AttackReduction;
    private float m_OriginalAttackPower;

    public WeaknessBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is WeaknessData weaknessData)
        {
            m_AttackReduction = weaknessData.AttackReduction;
        }
        else
        {
            m_AttackReduction = 0.3f; // 默认降低30%攻击���
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始攻击���
        // if (Target.AttrComponent != null)
        // {
        //     m_OriginalAttackPower = Target.AttrComponent.AttackPower;
        //     Target.AttrComponent.SetAttackPower(m_OriginalAttackPower * (1.0f - m_AttackReduction));
        // }
        
        // 播放虚弱特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("weakness", true);
        }
        
        Logger.Log($"{Target.name} 变得虚弱，攻击力降低 {m_AttackReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 虚弱期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 虚弱效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始攻击���
        // if (Target.AttrComponent != null)
        // {
        //     Target.AttrComponent.SetAttackPower(m_OriginalAttackPower);
        // }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("weakness", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Weakness"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 虚弱效果可以叠加，刷新持续时���
    }
}
